package express

import (
	"app/common/constant"
	"app/common/funcs"
	"app/common/params"
	"app/common/response"
	"app/internal/codec"
	"app/internal/model"
	"app/pkg/log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type TakeAddRequest struct {
	Code     string `json:"code" validate:"required" message:"required:请输入取件码" filter:"trim"`  //取件码
	Time     string `json:"time" validate:"required" message:"required:请选择送出时间" filter:"trim"` //送出时间
	Note     string `json:"note" filter:"trim"`                                                //备注
	Location string `json:"location" validate:"required" message:"required:请完善收件地址信息" filter:"trim"`
	Name     string `json:"name" validate:"required" message:"required:请完善收件人信息" filter:"trim"`
	Phone    string `json:"phone" validate:"required" message:"required:手机号不能为空" filter:"trim"`
}

// @Summary	新增取快递任务
// @Schemes
// @Tags		快递
// @Accept		json
// @Param		Authorization	header		string				true	"签名"
// @Param		payload			body		TakeAddRequest		true	"body"
// @success	200				{object}	constant.ErrInfo	"desc"
// @Router		/api/express/take-add [post]
func TakeAdd(c *gin.Context) {
	req := TakeAddRequest{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	uid := c.GetInt64("uid")
	err := model.Save[model.TakeExpressModel](&model.TakeExpressModel{
		UserId:   uid,
		Location: req.Location,
		Phone:    req.Phone,
		Name:     req.Name,
		Note:     req.Note,
		Time:     req.Time,
		Code:     req.Code,
		State:    1,
	})
	if err != nil {
		log.NewError("express.takeAdd", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "操作失败"})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}

type TakeEditRequest struct {
	params.ParamsMainKey
	Code     string `json:"code" filter:"trim"` //取件码
	Time     string `json:"time" filter:"trim"` //送出时间
	Note     string `json:"note" filter:"trim"` //备注
	Location string `json:"location"  filter:"trim"`
	Name     string `json:"name" filter:"trim"`
	Phone    string `json:"phone"  filter:"trim"`
	State    int8   `json:"state"` //1 待处理 2已完成 3异常
	Msg      string `json:"msg"`   //异常原因
}

// @Summary	修改取快递任务
// @Schemes
// @Tags		快递
// @Accept		json
// @Param		Authorization	header		string				true	"签名"
// @Param		payload			body		TakeEditRequest		true	"body"
// @success	200				{object}	constant.ErrInfo	"desc"
// @Router		/api/express/take-edit [post]
func TakeEdit(c *gin.Context) {
	req := TakeEditRequest{}
	if err := codec.GinBind(c, &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	old, err := model.FindOne[model.TakeExpressModel]("id=?", req.Id)
	if err != nil {
		log.NewError("express.TakeEdit", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: "获取数据失败"})
		return
	}
	if req.Note != "" {
		old.Note = req.Note
	}
	if req.Location != "" {
		old.Location = req.Location
	}
	if req.Code != "" {
		old.Code = req.Code
	}
	if req.Time != "" {
		old.Time = req.Time
	}
	if req.Name != "" {
		old.Name = req.Name
	}
	if req.Phone != "" {
		old.Phone = req.Phone
	}
	if req.State > 0 {
		old.State = req.State
	}
	if req.Msg != "" {
		old.Msg = req.Msg
	}
	err = model.Save[model.TakeExpressModel](&old)
	if err != nil {
		log.NewError("location.Edit", err)
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.LogicError, Msg: "操作失败"})
		return
	}
	c.JSON(http.StatusOK, constant.OK)
}

type TasksRequest struct {
	params.ParamsPagination
	State     int8   `form:"state"`
	Uid       string `form:"uid"`       //用户ID
	TimeStart string `form:"timeStart"` //开始时间
	TimeEnd   string `form:"timeEnd"`   //结束时间
	Keyword   string `form:"keyword"`   //关键词
}

// @Summary	取件记录
// @Schemes
// @Description
// @Tags		快递
// @Accept		mpfd
// @Param		Authorization	header		string												true	"签名"
// @Param		limit			query		int													true	"数量"
// @Param		page			query		int													true	"页码"
// @Param		keyword			query		string												false	"关键词"
// @Param		timeStart		query		string												false	"开始时间"
// @Param		timeEnd			query		string												false	"结束时间"
// @Param		state			query		int													false	"1 待处理 2已完成 3异常"
// @success	200				{object}	response.JSONResult{data=[]model.TakeExpressModel}	"desc"
// @Router		/api/express/takes [GET]
func Takes(c *gin.Context) {
	req := TasksRequest{}
	if err := codec.GinBind(c.Copy(), &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	list, pagination, _ := model.NewTakeExpressModel().List(c.GetInt64("uid"), req.Keyword, req.State, req.Page, req.Limit, req.TimeStart, req.TimeEnd)
	c.JSON(http.StatusOK, response.JSONPaginateResult{Paginate: pagination, Data: list})
}

// @Summary	取件管理
// @Schemes
// @Description
// @Tags		快递-管理员端
// @Accept		mpfd
// @Param		Authorization	header		string												true	"签名"
// @Param		limit			query		int													true	"数量"
// @Param		page			query		int													true	"页码"
// @Param		keyword			query		string												false	"关键词"
// @Param		uid				query		string												false	"用户ID"
// @Param		timeStart		query		string												false	"开始时间"
// @Param		timeEnd			query		string												false	"结束时间"
// @Param		state			query		int													false	"1 待处理 2已完成 3异常"
// @success	200				{object}	response.JSONResult{data=[]model.TakeExpressModel}	"desc"
// @Router		/manager/express/takes [GET]
func ManagerTakes(c *gin.Context) {
	req := TasksRequest{}
	if err := codec.GinBind(c.Copy(), &req); err != nil {
		c.JSON(http.StatusOK, constant.ErrInfo{Code: constant.ParamsError, Msg: err.Error()})
		return
	}
	queryUid := int64(0)
	if req.Uid != "" {
		queryUid = funcs.IdDecode(req.Uid)
	}
	list, pagination, _ := model.NewTakeExpressModel().List(queryUid, req.Keyword, req.State, req.Page, req.Limit, req.TimeStart, req.TimeEnd)
	c.JSON(http.StatusOK, response.JSONPaginateResult{Paginate: pagination, Data: list})
}

// @Summary	取快递任务修改
// @Schemes
// @Tags		快递-管理员端
// @Accept		json
// @Param		Authorization	header		string				true	"签名"
// @Param		payload			body		TakeEditRequest		true	"body"
// @success	200				{object}	constant.ErrInfo	"desc"
// @Router		/manager/express/take-edit [post]
func ManagerTakeEdit(c *gin.Context) {
	TakeEdit(c)
}
